// Author: InfiniteStarlight
#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)((v).size())
#define ALL(v) (v).begin(),(v).end()
#define rALL(v) (v).rbegin(),(v).rend()
#define srt(v) sort(ALL(v))
#define rsrt(v) sort(rALL(v))
#define lb(v,x) (int)(lower_bound(ALL(v),x)-(v).begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-(v).begin())
#define uni(v) (v).resize(unique(ALL(v))-(v).begin())
#define rev(v) reverse(ALL(v))
#define mp make_pair
#define pb emplace_back
#define inf 0x3f3f3f3f
using pii=pair<int,int>;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128_t;
using u128=__uint128_t;
int a[200200];
ll f[1000100];
ll global;
int main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	for(int i=0;i<1000100;i++)
		f[i]=-1ll*inf*inf;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		f[0]=global=0;
		ll mx=0;
		for(int i=2;i<=n;i++)
		{
			ll val=global+max(mx,f[a[i]]+a[i]);
			if(a[i]==a[i-1]) global+=a[i];
			mx=max(mx,val-global);
			f[a[i-1]]=max(f[a[i-1]],val-global);
		}
		cout<<mx+global<<'\n';
		for(int i=1;i<=n;i++)
			f[a[i]]=-1ll*inf*inf;
	}
	return 0;
}